var server = require('api');
var _alert = require('alert');
var _clear = require('clearForm');

var office = avalon.define({
	$id :'office',
    userData: [],
	info: '',
	add : {
		name: '',
		note: '',
		confirm: function () {
			var data = {
                userId: avalon.vmodels.root.user.id,
				userToken: avalon.vmodels.root.user.token,
                depTypeName: office.add.name,
                depTypeNote: office.add.note
			}
			$.ajax({
				url: server.addOfficeType,
				type: 'post',
				dataType: 'json',
				data: JSON.stringify(data),
                headers: {
                    'Content-Type': 'application/json'
                }
			})
				.done(function (res) {
					if(res.status == '9200'){
                        _alert('创建科室类型成功');
                        $('#addOfficeTypeModal').modal('hide');
                        queryType();
					}else{
						_alert('创建科室类型失败 ' + res.description);
					}

                })
                .fail(function () {
                    _alert('网络错误，创建科室类型失败')
                })
        }
	},

	//modal
    delOfficeModal: function () {

        if (office.info){
            $('#delOfficeModal').modal('show');
        }else {
            _alert('请先选择科室')
        }

    },

    delOffice: function () {

        $.ajax({
            url: server.delOfficeType + 'userId=' + avalon.vmodels.root.user.id + '&userToken=' + avalon.vmodels.root.user.token
            + '&depTypeId=' + office.info.depTypeId,
            type: 'delete',
            dataType: 'json',
        })
            .fail(function(res) {
                $('#table').bootstrapTable({});
                _alert('网络错误，请重试');
            })
            .done(function(res) {
                var data = res.values;
                $('#delOfficeModal').modal('hide');
                if(res.status == '9200'){
                    _alert('删除成功');
                    queryType();
                }else{
                    $('#delOfficeModal').modal('hide');
                    queryType();
                    _alert(res.description);
                }
                // userTable.userData = data.values;

            });
    },

	init :function(argument) {
		avalon.log('科室类型init' + _.now());
		setTimeout(function () {
			$(function () {
                queryType();
            })
        },100)
	}
})

//查询科室类型
function queryType() {

    office.info = '';

    $.ajax({
        url: server.queryOffice,
        type: 'get',
        dataType: 'json',
        data: {userId: avalon.vmodels.root.user.id,userToken:avalon.vmodels.root.user.token}
    })
        .done(function (res) {
            if(res.status == '9200'){
                var data = res;
                office.userData = data.values;
                $('#table').bootstrapTable({});

                setTimeout(function () {
                    $('#table').bootstrapTable('load', data.values);
                    $('#table').bootstrapTable('selectPage', 1);

                    //hide
                    $('#addOfficeTypeModal').on('show.bs.modal', function (e) {
                        _clear($('#addOfficeTypeModal'));
                    })

                    $('#table').on('check.bs.table', function (e, row,ele) {
                        office.info = row;
                    })

                },10)
            }else {
                _alert(res.description)
                $('#table').bootstrapTable({});
            }
        })
        .fail(function (res) {
            _alert('网络错误，查询科室失败');
            $('#table').bootstrapTable({});
        })
}

module.exports = office;